{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Realtime Text Extracion.ipynb",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "lv8kfltaKySW"
      },
      "source": [
        "##**TEXT EXTRACTION FROM A REAL TIME CAPTURED IMAGE**"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "wWB6a7k7IQ4W"
      },
      "source": [
        "###**Code for starting the webcam in the google colab**"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "SucxddsPhOmj"
      },
      "source": [
        "from IPython.display import display, Javascript\n",
        "from google.colab.output import eval_js\n",
        "from base64 import b64decode\n",
        "\n",
        "def take_photo(filename='photo.jpg', quality=0.8):\n",
        "  js = Javascript('''\n",
        "    async function takePhoto(quality) {\n",
        "      const div = document.createElement('div');\n",
        "      const capture = document.createElement('button');\n",
        "      capture.textContent = 'Capture';\n",
        "      div.appendChild(capture);\n",
        "\n",
        "      const video = document.createElement('video');\n",
        "      video.style.display = 'block';\n",
        "      const stream = await navigator.mediaDevices.getUserMedia({video: true});\n",
        "\n",
        "      document.body.appendChild(div);\n",
        "      div.appendChild(video);\n",
        "      video.srcObject = stream;\n",
        "      await video.play();\n",
        "\n",
        "      // Resize the output to fit the video element.\n",
        "      google.colab.output.setIframeHeight(document.documentElement.scrollHeight,true);\n",
        "\n",
        "      // Wait for Capture to be clicked.\n",
        "      await new Promise((resolve) => capture.onclick = resolve);\n",
        "\n",
        "      const canvas = document.createElement('canvas');\n",
        "      canvas.width = video.videoWidth;\n",
        "      canvas.height = video.videoHeight;\n",
        "      canvas.getContext('2d').drawImage(video, 0, 0);\n",
        "      stream.getVideoTracks()[0].stop();\n",
        "      div.remove();\n",
        "      return canvas.toDataURL('image/jpeg', quality);\n",
        "    }\n",
        "    ''')\n",
        "  display(js)\n",
        "  data = eval_js('takePhoto({})'.format(quality))\n",
        "  binary = b64decode(data.split(',')[1])\n",
        "  with open(filename, 'wb') as f:\n",
        "    f.write(binary)\n",
        "  return filename"
      ],
      "execution_count": 4,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "KxGGlrg-K_Wz"
      },
      "source": [
        "####Click on the **Capture** button for capturing the image"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "buJCl90WhNfq"
      },
      "source": [
        "from IPython.display import Image\n",
        "try:\n",
        "  filename = take_photo()\n",
        "  print('Saved to {}'.format(filename))\n",
        "  \n",
        "  # Show the image which was just taken.\n",
        "  display(Image(filename))\n",
        "except Exception as err:\n",
        "  # Errors will be thrown if the user does not have a webcam or if they do not\n",
        "  # grant the page permission to access it.\n",
        "  print(str(err))"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "V-95YMbaIjCL"
      },
      "source": [
        "###**Installing and importing the required libraries**"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "TPetMlwdvspP"
      },
      "source": [
        "!sudo apt install tesseract-ocr\r\n",
        "!pip install pytesseract\r\n",
        "import pytesseract as ts\r\n",
        "import cv2\r\n",
        "from google.colab.patches import cv2_imshow"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "c5UJmjW8wbZ9"
      },
      "source": [
        "'''\r\n",
        "If there is error occured as path is not defined or you get the wrong image\r\n",
        "Follow the given steps\r\n",
        "Put the path of the picture captured by you\r\n",
        "You can find it in the files part under the navigation in the left side\r\n",
        "Right click on the photo, click on \"copy path\" and paste it here \r\n",
        "'''\r\n",
        "\r\n",
        "img=cv2.imread('/content/photo.jpg')\r\n",
        "gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\r\n",
        "cv2_imshow(img)\r\n",
        "#Removing noise from the image\r\n",
        "ret, thresh = cv2.threshold(gray,1010, 200, cv2.THRESH_OTSU, cv2.THRESH_BINARY)\r\n",
        "text = ts.image_to_string(thresh)  \r\n",
        "\r\n",
        "cv2_imshow(thresh)   #printing realtime text\r\n",
        "print(\"\\n\\n\")\r\n",
        "print(text)\r\n",
        "\r\n",
        "#If the desried output is not obtained just change the 'thresh to 'gray' in line 13 and 15"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "fdVdr4Ka05FG"
      },
      "source": [
        ""
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "jUyVjAV9w3iG"
      },
      "source": [
        ""
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}